package com.example.mediaplayer;


import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;


public class MainActivity extends Activity 
{
	private ImageButton imageBtn_Previous;
	private ImageButton imageBtn_PlayOrPause;
	private ImageButton imageBtn_Stop;
	private ImageButton imageBtn_Next;
	private ListView list;
	private int number;
	
private MediaPlayer player;
	
	private void load(int number)
	{
		if(player != null)
		{
			player.release();
		}
		Uri musicUri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,"" + number);
		player = MediaPlayer.create(this,musicUri);
	}
	private void play(int number)
	{
		if(player != null && player.isPlaying())
		{
			player.stop();
		}
		load(number);
		player.start();
	}
	private void pause()
	{
		if(player.isPlaying())
		{
			player.pause();
		}
	}
	private void stop()
	{
		if(player != null)
		{
			player.stop();
		}
	}
	/*
	private void resume()
	{
		player.start();
	}
	private void replay()
	{
		player.start();
	}
	*/
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findViews();
		registerListeners();
		number = 1;
		
	}
	private void findViews()
	{
		imageBtn_Previous = (ImageButton) findViewById(R.id.imageButton1);
		imageBtn_PlayOrPause = (ImageButton) findViewById(R.id.imageButton2);
		imageBtn_Stop = (ImageButton) findViewById(R.id.imageButton3);
		imageBtn_Next = (ImageButton) findViewById(R.id.imageButton4);
		list = (ListView) findViewById(R.id.listView1);
	}
	private void registerListeners()//����������
	{
		imageBtn_Previous.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				moveNumberToPrevious();
				play(number);
				imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_pause);	
			}
		});
		imageBtn_PlayOrPause.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				if(player != null && player.isPlaying())
				{
					pause();
					imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_play);
				}
				else
				{
					play(number);
					imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_pause);
				}
					
			}
		});
		imageBtn_Stop.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				stop();
				imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_play);
			}
		});
		imageBtn_Next.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				moveNumberToNext();
				play(number);
				imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_pause);
			}
		});
		list.setOnItemClickListener(new OnItemClickListener()//����б�����
		{
			public void onItemClick(AdapterView<?>parent,View view,int position,long id)
			{
				number = position +1;
				play(number);
				imageBtn_PlayOrPause.setBackgroundResource(R.drawable.button_pause);
			}
		});
	}
	protected void onResume()
	{
		super.onResume();
		initMusicList();
		if(list.getCount() == 0)
		{
			imageBtn_Previous.setEnabled(false);
			imageBtn_PlayOrPause.setEnabled(false);
			imageBtn_Stop.setEnabled(false);
			imageBtn_Next.setEnabled(false);
			Toast.makeText(this,this.getString(R.string.tip_no_music_file),Toast.LENGTH_SHORT).show();
		}
		else
		{
			imageBtn_Previous.setEnabled(true);
			imageBtn_PlayOrPause.setEnabled(true);
			imageBtn_Stop.setEnabled(true);
			imageBtn_Next.setEnabled(true);
		}
	}
	
	private void initMusicList()
	{
		Cursor cursor = getMusicCursor();
		setListContent(cursor);
	}
	private void setListContent(Cursor musicCursor)
	{
		@SuppressWarnings("deprecation")
		CursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,musicCursor,new String[]
				{
					MediaStore.Audio.AudioColumns.TITLE,
					MediaStore.Audio.AudioColumns.ARTIST
				},new int[]
						{
							android.R.id.text1,android.R.id.text2
						});
		list.setAdapter(adapter);
	}
	private Cursor getMusicCursor()
	{
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
		return cursor;
	}
	
	
	private void moveNumberToNext()
	{
		if((number+1) > list.getCount())
		{
			number = 1;
			Toast.makeText(MainActivity.this,MainActivity.this.getString(R.string.tip_reach_bottom),Toast.LENGTH_SHORT).show();
		}
		else
		{
			++number;
		}
	}
	private void moveNumberToPrevious()
	{
		if(number == 1)
		{
			number = list.getCount();
			Toast.makeText(MainActivity.this,MainActivity.this.getString(R.string.tip_reach_top),Toast.LENGTH_SHORT).show();
		}
		else
		{
			--number;
            /*哈哈*/
		}
	}

}
